//
//  WSNoticeViewController.swift
//  微校园
//
//  Created by Gao on 16/12/12.
//  Copyright © 2016年 weSchool. All rights reserved.
//

import UIKit

/// 通知可重用 cell id
private let noticeCellId = "NoticeCell"
private let blankCellId = "blankCell"


class WSNoticeViewController: WSBaseViewController {
    
    /// 表格视图
    var tableView: UITableView?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    

}

// MARK: - 设置界面
fileprivate extension WSNoticeViewController {
    
    func setupUI() {
        view.backgroundColor = UIColor.lightGray

        setupNavigationBar()
        setupTableView()

    }
    
    /// 设置表格视图 - 用户登录之后执行
    func setupTableView() {
        
        
        let viewFrame = view.bounds
        let tableViewX = viewFrame.origin.x + 5
        let tableViewY = viewFrame.origin.y
        let tableViewW = viewFrame.size.width - 10
        let tableViewH = viewFrame.size.height
        
        let tableViewFrame = CGRect(x: tableViewX, y: tableViewY, width: tableViewW, height: tableViewH)
        
        tableView = UITableView(frame: tableViewFrame, style: .plain)
        tableView?.backgroundColor = view.backgroundColor
        
        if let navigationBar = navigationController?.navigationBar {
            view.insertSubview(tableView!, belowSubview: navigationBar)
        }else{
            view.addSubview(tableView!)
        }
        // 取消自动缩进 - 如果隐藏了导航栏，会缩进 20 个点
        automaticallyAdjustsScrollViewInsets = true
        
        // 修改指示器的缩进 - 强行解包是为了拿到一个必有的 inset
        tableView?.scrollIndicatorInsets = tableView!.contentInset
        
        // 注册原型 cell
        tableView?.register(UINib(nibName: "WSNoticeTableViewCell", bundle: nil), forCellReuseIdentifier: noticeCellId)
        
        tableView?.delegate = self
        tableView?.dataSource = self
        
        // 取消分隔线
        tableView?.separatorStyle = .none

        
    }
    
    /// 设置导航条
    func setupNavigationBar() {
        // 设置左侧导航栏按钮
        navigationItem.leftBarButtonItem =  UIBarButtonItem(title: "全部▼ ", style: .plain, target: self, action: #selector(selectSearchItem))
        
        // 设置查询框
        let searchBarRect = CGRect(x: 0, y: 0, width: 320, height: 44 )
        let customSearchBar = UISearchBar(frame: searchBarRect)
        customSearchBar.placeholder = "请输入查询条件"
        navigationItem.titleView = customSearchBar
        
        // 设置右侧导航栏按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add,
                                                            target:self,
                                                            action:nil)
        
        

    }


    
    @objc func selectSearchItem(){
        
        //配置一：基础配置
        KxMenu.setTitleFont(UIFont(name: "HelveticaNeue", size: 15))
        
        //配置二：拓展配置
        let options = OptionalConfiguration(arrowSize: 9,  //指示箭头大小
            marginXSpacing: 7,  //MenuItem左右边距
            marginYSpacing: 9,  //MenuItem上下边距
            intervalSpacing: 25,  //MenuItemImage与MenuItemTitle的间距
            menuCornerRadius: 6.5,  //菜单圆角半径
            maskToBackground: true,  //是否添加覆盖在原View上的半透明遮罩
            shadowOfMenu: false,  //是否添加菜单阴影
            hasSeperatorLine: true,  //是否设置分割线
            seperatorLineHasInsets: false,  //是否在分割线两侧留下Insets
            textColor: Color(R: 0, G: 0, B: 0),  //menuItem字体颜色
            menuBackgroundColor: Color(R: 1, G: 1, B: 1)  //菜单的底色
        )
        
        var menuItems = [KxMenuItem]()
        
        if navigationItem.leftBarButtonItem?.title != "全部▼ " {
            menuItems.append(KxMenuItem("全部", image: nil, target: self, action:  #selector(searchAll)))
        }
        
        if navigationItem.leftBarButtonItem?.title != "收到▼ " {
            menuItems.append(KxMenuItem("收到", image: nil, target: self, action:  #selector(searchReceive)))
        }
        
        if navigationItem.leftBarButtonItem?.title != "发出▼ " {
               menuItems.append(KxMenuItem("发出", image: nil, target: self, action:  #selector(searchSend)))
        }
        

        KxMenu.show(in: self.view, from: CGRect(x: 0, y: 0, width: 100, height: 64 ), menuItems: menuItems, withOptions: options )
        
    }
    
    
    @objc func searchAll() {
        
        navigationItem.leftBarButtonItem?.title = "全部▼ "
    }

    @objc func searchReceive() {
        
        navigationItem.leftBarButtonItem?.title = "收到▼ "
    
    }
    
    @objc func searchSend() {
        
        navigationItem.leftBarButtonItem?.title = "发出▼ "
        
        
    }
    
    
    
}

// MARK: - 表格数据源方法，具体的数据源方法实现，不需要 super
extension WSNoticeViewController : UITableViewDataSource, UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return self.cards.value(forKeyPath: "cards")!.count * 2 - 1
        return 10
    }
    

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row % 2 == 0 {
            return 10
        }
        return 65
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
        if indexPath.row % 2 == 0 {
            let cell2 = UITableViewCell(style: .default, reuseIdentifier: blankCellId)
            cell2.contentView.alpha = 0
            cell2.isUserInteractionEnabled = false
            cell2.backgroundColor = tableView.backgroundColor
            
            return cell2
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: noticeCellId, for: indexPath) as! WSNoticeTableViewCell
        
        // 2. 设置 cell
        //        cell.viewModel = vm
        
        cell.cornerRadius = 10
        return cell
    }

    
    
}
